﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Drawing;
using System.Drawing.Imaging;
using System.IO;

namespace JULONG.WEB.Library
{
    /// <summary>
    /// 图片验证码
    /// </summary>
    public class pictureCode
    {
        /// <summary>
        /// 图片大小
        /// </summary>
        public string pictureSize = "80*25";

        /// <summary>
        /// 背景色
        /// </summary>
        public Color bgColor = Color.White;

        /// <summary>
        /// 噪音线数目
        /// </summary>
        public int lineCount = 12;

        /*

                //字符色
                public string fontColor = "#ffffff";
                //字符大小
                public int fontSize = 14;
                //字体字典
                public string fontDict = "Arial";

        */


        //*********************************
        Random rand = new Random();
        Bitmap Bitmap;
        Graphics picture;

        private string strDict;
        private Font font = new Font("Arial", 15, FontStyle.Bold | FontStyle.Italic);
        private int w
        {
            get
            {
                try
                {
                    string[] size = pictureSize.Split('*');
                    return Convert.ToInt16(size[0]);
                }
                catch
                {
                    return 60;
                }
            }
        }
        private int h
        {
            get
            {
                try
                {
                    string[] size = pictureSize.Split('*');
                    return Convert.ToInt16(size[1]);
                }
                catch
                {
                    return 60;
                }
            }
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="strDict"></param>
        public pictureCode(string strDict = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
        {
            this.strDict = strDict;
        }


        /// <summary>
        /// 设置图片
        /// </summary>
        private void set()
        {
            Bitmap = new Bitmap(w, h);
            picture = Graphics.FromImage(Bitmap);
            System.Web.UI.WebControls.WebColorConverter ww = new System.Web.UI.WebControls.WebColorConverter();
            picture.Clear(bgColor);
        }

        /// <summary>
        /// 画图片的背景噪音线
        /// </summary>
        private void randomLine()
        {
            for (int i = 0; i < lineCount; i++)
            {
                int x1 = rand.Next(w);
                int x2 = rand.Next(w);
                int y1 = rand.Next(h);
                int y2 = rand.Next(h);
                picture.DrawLine(new Pen(Color.LightGray), x1, y1, x2, y2);
            }
        }

        /// <summary>
        /// 画字符
        /// </summary>
        /// <param name="code"></param>
        private void writeDtr(string code)
        {
            //string code = this.code();
            System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(
                new Rectangle(0, 0, w, h), Color.Blue, Color.Gray, 1.2f, true);
            picture.DrawString(code, font, brush, 0, 0);
        }

        /// <summary>
        /// 生成
        /// </summary>
        /// <param name="code"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        public MemoryStream get(out string code, int length = 5)
        {
            char[] str = new char[length];
            for (int i = 0; i < length; i++)
                str[i] = strDict[rand.Next(strDict.Length)];
            code = new string(str);
            set();
            randomLine();
            writeDtr(code);
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            Bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
            return ms;
        }

    }
}
